var __extends =
    (this && this.__extends) ||
    (function () {
      var e = function (t, r) {
        return (e =
          Object.setPrototypeOf ||
          ({ __proto__: [] } instanceof Array &&
            function (e, t) {
              e.__proto__ = t;
            }) ||
          function (e, t) {
            for (var r in t)
              Object.prototype.hasOwnProperty.call(t, r) && (e[r] = t[r]);
          })(t, r);
      };
      return function (t, r) {
        function s() {
          this.constructor = t;
        }
        if ("function" != typeof r && null !== r)
          throw new TypeError(
            "Class extends value " + String(r) + " is not a constructor or null"
          );
        e(t, r),
          (t.prototype =
            null === r
              ? Object.create(r)
              : ((s.prototype = r.prototype), new s()));
      };
    })(),
  List = Laya.List,
  Handler = Laya.Handler,
  Label = Laya.Label,
  ServerListView = (function (e) {
    function t() {
      var t = e.call(this) || this;
      (t.selectIndex = 0), (t.isLoading = 0);
      return (
        t.bt_gameStart.on(Laya.Event.CLICK, t, t.loginGame),
        (t.serverList.array = []),
        (t.serverList.selectEnable = !0),
        t.serverList.on(Laya.Event.CLICK, t, t.onServerListDidClick),
        (t.serverList.renderHandler = new Handler(t, t.onRender)),
        t.btn_serverListBack.on(Laya.Event.CLICK, t, t.onServerListBackPreFn),
        t
      );
    }
    return (
      __extends(t, e),
      (t.prototype.init = function () {}),
      (t.openServerListView = function () {
        return (
          null == this.serverlistVC && (this.serverlistVC = new t()),
          this.serverlistVC.loadServerList(),
          (this.serverlistVC.serverList.visible = !1),
          (this.serverlistVC.selectIndex = 0),
          (this.serverlistVC.serverList.selectedIndex = 0),
          this.serverlistVC
        );
      }),
      (t.prototype.onServerListDidClick = function (e) {
        "" != e.target.name &&
          (this.selectIndex = this.serverList.selectedIndex);
      }),
      (t.prototype.onRender = function (e, t) {
        var r = e.getChildByName("img_norServer");
        t == this.selectIndex
          ? (r.skin = "comp/login/item_bg_sel.png")
          : (r.skin = "comp/login/item_bg.png");
      }),
      (t.prototype.onServerListBackPreFn = function () {
        laya.net.LocalStorage.removeItem("accessToken"),
          laya.net.LocalStorage.removeItem("loginTime"),
          this.removeSelf();
        var e = new LoginView();
        Laya.stage.addChild(e);
      }),
      (t.prototype.loadServerList = function () {
        var e = new Ajax(this);
        (e.success = this.loadServerListSuccess),
          e.get(Environment.apiHost + "/user-service/area/list", null);
      }),
      (t.prototype.loadServerListSuccess = function (e) {
        if (0 == e.code) {
          if (null != e.data) {
            this.list = e.data;
            for (var t, r = [], s = 0; s < this.list.length; s++)
              this.list[s].descr,
                (t = {
                  serverName: { text: this.list[s].name },
                  server_suggest: { text: "推荐" },
                }),
                r.push(t);
            (this.serverList.array = r), (this.serverList.visible = !0);
          }
        } else ToastView.toastView(e.msg);
      }),
      (t.prototype.loginGame = function () {
        if (-1 != this.selectIndex)
          if (2 != this.isLoading) {
            if (!(1 == this.isLoading && this.list.length > 0) && this.list) {
              1 != this.list[this.selectIndex].state
                ? this.loadGameStartRecourse()
                : ToastView.toastView("服务器尚未开启");
            }
          } else this.onLoad();
        else ToastView.toastView("请选择服务区");
      }),
      (t.prototype.loadGameStartRecourse = function () {
        this.isLoading = 1;
        var e = [
          { url: "res/atlas/map.atlas", type: Laya.Loader.ATLAS },
          { url: "res/atlas/comp.atlas", type: Laya.Loader.ATLAS },
          { url: "res/atlas/comp/Server.atlas", type: Laya.Loader.ATLAS },
          { url: "res/atlas/comp/Home.atlas", type: Laya.Loader.ATLAS },
          { url: "res/atlas/comp/goods.atlas", type: Laya.Loader.ATLAS },
          { url: "res/atlas/comp/hreoImg.atlas", type: Laya.Loader.ATLAS },
          { url: "res/atlas/comp/commonpic.atlas", type: Laya.Loader.ATLAS },
        ];
        Laya.loader.load(
          e,
          Laya.Handler.create(this, this.onLoad),
          Laya.Handler.create(
            AXProgressBarView,
            AXProgressBarView.onLoadedRecourse,
            null,
            !1
          )
        );
      }),
      (t.prototype.onLoad = function () {
        this.isLoading = 2;
        var e = this.list[this.selectIndex],
          t = e.mod_dstid;
        if (Environment.subarea.indexOf(".com") > -1) {
          r = void 0;
          (r =
            t.indexOf("wss") > -1
              ? t.replace("wss", "https").replace("/user-service/websocket", "")
              : t.replace("ws", "http").replace("/user-service/websocket", "")),
            (Environment.subarea = r);
        }
        if (Browser.window.location.href.indexOf("g.") > -1) {
          var r;
          (r = (r = Environment.subarea).replace("s.", "g.")),
            (t = t.replace("s.", "g.")).indexOf("8001") > -1 &&
              ((t = t
                .replace(":8001", "")
                .replace("/user-service", "/9001/user-service")),
              (r = r.replace(":8001", "/9001"))),
            t.indexOf("8002") > -1 &&
              ((t = t
                .replace(":8002", "")
                .replace("/user-service", "/9002/user-service")),
              (r = r.replace(":8002", "/9002"))),
            t.indexOf("8003") > -1 &&
              ((t = t
                .replace(":8003", "")
                .replace("/user-service", "/9003/user-service")),
              (r = r.replace(":8003", "/9003"))),
            (Environment.subarea = r);
        }
        laya.net.LocalStorage.setItem("serverName", e.name),
          laya.net.LocalStorage.setItem("serverId", e.id),
          (Global.areaId = e.id);
        var s = laya.net.LocalStorage.getItem("accessToken");
        WebSocketService.open(t + "?t=" + s),
          this.removeSelf(),
          SelectRoleView.open();
      }),
      (t.prototype.loadProgressComplete = function (e) {
        if (1 == e) {
          this.isLoading = 2;
          var t = this.list[this.selectIndex],
            r = laya.net.LocalStorage.getItem("accessToken");
          WebSocketService.open(t.mod_dstid + "?t=" + r),
            this.removeSelf(),
            SelectRoleView.open();
        }
      }),
      t
    );
  })(ui.ServerListUI);
